
/**
 * name:        绑定弹窗
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */

$(function(){
    var po_2=''
    po_2+='<div class="popover_m popover_m1" style="display:none"></div>'
    po_2+='<div class="site_bg site_bg1" style="display:none">'
    po_2+='<span class="gb_01" style="display:block;width:18px;height:18px;background:url(../../img/base/icon_06.png) 0 0 no-repeat;float:right;margin:40px 30px 0 0;cursor: pointer; position: absolute; z-index: 1;right: 30px"></span>'
    po_2+='<div class="register_bg" style="width:100%; border:0; padding:0;">'
    po_2+='<p class="login_title" style=" height:89px; line-height:89px; color:#fff; font-weight:600;">绑定手机</p>'
    po_2+='<ul style="margin-top:50px">'
    po_2+='<li>'
    po_2+='<em>手机号码</em>'
    po_2+='<input type="text" name="phone" id="phone1" placeholder="您的手机号码">'
    po_2+='<div class="error">请输入正确的手机号</div>'
    po_2+='</li>'
    po_2+='<li>'
    po_2+='<em>验<i style="width: 6px; display: inline-block;"></i>证<i style="width: 6px; display: inline-block;"></i>码</em>'
    po_2+='<input type="text" name="phoneCode" id="verification" placeholder="请输入验证码">'
    po_2+='<a id="phoneCode">获取短信验证码</a>'
    po_2+='<div class="error">验证码错误</div>'
    po_2+='</li>'           
    po_2+='</ul>'
    po_2+='<div class="error_text"></div>'
    po_2+='<div class="login_sub"><input type="submit" value="提交" id="login_sub"></div>   '
    po_2+='</div> '
    po_2+='</div>'
    $('body').append(po_2)
})
function ajax_form(){
    if($.cookie('user')!=''&&$.cookie('user')!=null){
        strs=$.cookie('user').split(",")
        var token=''
        for(var i in strs){
            token=strs[6]
        }
    $('#phoneCode').click(function(){
        var phone = $('#phone1').val();
        var url= new_url+"shop/login/sendPhoneCode"; 
        var time='60'
        function tim(){
            setTimeout(function(){                                               
            if(time>'0'){
                tim();
                time --
                $('#phoneCode').text(time+'s重新获取');
            }
            else{
                $('#phoneCode').text('重新获取验证码')  
            }    
            },1000)
        }
        if((/^1(3|4|5|7|8)\d{9}$/.test(phone))){
            tim();
            $.ajax({
                type: "post",
                dataType:'json',
                url: url, 
                data:'phone='+phone,       
                success: function(e){ 
                    if(e.code=='0'){
                        tc(e.message);  
                    }
                    else{
                        tc(e.message);
                    }
                }
            });
        }
        else{
            tc('请输入手机号')
        }
    })
    function phone(){    
        var phone = $('#phone1').val();  
            if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
                $('#phone1').parents('li').addClass('error_bor');
                $('.error_bor').find('.error').show();
                return false;     
            }else{
                $('#phone1').parents('li').removeClass('error_bor');
                $('#phone1').parents('li').find('.error').hide();
                // return false;
                };    
            if($('#verification').val()==''){
                $('#verification').parents('li').addClass('error_bor'); 
                $('.error_bor').find('.error').show();
                return false; 
            }else{
                $('#verification').parents('li').removeClass('error_bor');
                $('#verification').parents('li').find('.error').hide()
                // return false;
                };           
    }
    $('#login_sub').on('click',function(){
        phone(); 
        $('.register_bg ul li input').each(function(index,t){
        $(t).blur(function(){
            phone();
        })
    })
        if($('.register_bg ul li').hasClass('error_bor')){
            return false;
        }else{
            var phone2 = $('#phone1').val();
            var phoneCode=$('#verification').val(); 
            $.ajax({
            type:'post',
            dataType: 'json',
            data:{isBind:'1',phoneCode:phoneCode,phone:phone2,token:token},
            url:new_url+'shop/oauth/bindMember',
            success:function(e){ 
                if(e.code=='0'){
                    var mycars = new Array()
                        mycars[0] = e.data.name
                        mycars[1] = e.data.nickName
                        mycars[2] = e.data.phone
                        mycars[3] = e.data.avatar
                        mycars[4] = e.data.truename
                        mycars[5] = e.data.memberId
                        mycars[6] = e.data.token
                        mycars[7] = 
                    $.cookie('user',mycars,{path:'/'});    
                    tc(e.message)
                    setTimeout(function(){
                        window.location.reload()
                    },200)
                }
                else{
                    tc(e.message)
                }
            },
        })
        }
        return false;
    })
}
    }